import { createApp, defineAsyncComponent, ref } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store/init'; // store 存储仓库
import ElementPlus, { ElNotification } from 'element-plus';
import 'element-plus/dist/index.css';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import FluentSave28regular from '~icons/fluent/save-28-regular';
import FluentDown28regular from '~icons/fluent/contract-down-left-28-regular';
import FluentAdd28regular from '~icons/fluent/note-add-28-regular';
import RiReceived24 from '~icons/ri/folder-received-line';
import FluentCheckd24 from '~icons/fluent/mail-inbox-checkmark-24-regular';
import FluentUnCheckd24 from '~icons/fluent/mail-inbox-dismiss-24-regular';
import FluentQiTaChuRuKu24 from '~icons/fluent/table-switch-24-regular';
import FluentXiuGaiJiBenKuCun24 from '~icons/fluent/keyboard-123-24-regular';
import FluentCancel24 from '~icons/fluent/backspace-24-regular';
import FluentAdd24 from '~icons/fluent/apps-add-in-24-regular';
import FluentEdit24 from '~icons/fluent/draw-shape-24-regular';
import FluentDelete28 from '~icons/fluent/delete-28-regular';
import FluentComplete24 from '~icons/fluent/checkmark-square-24-regular';
import FluentLock24 from '~icons/fluent/lock-multiple-24-regular';

import 'animate.css';
import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';

//公共组件
import components from '@/components';
import api from './api'; // 导入api接口
import useStore, { registerStore } from '@/store';
import Directive from '@/plugins/directive';
import SignalR from './utils/signalR';
import { MessageTypeEnum } from './enums';
const app = createApp(App);
Object.keys(ElementPlusIconsVue).forEach(key => {
  app.component(key, ElementPlusIconsVue[key as keyof typeof ElementPlusIconsVue]);
});
app.component(FluentSave28regular.name, FluentSave28regular);
app.component(FluentDown28regular.name, FluentDown28regular);
app.component(FluentAdd28regular.name, FluentAdd28regular);
app.component(RiReceived24.name, RiReceived24);
app.component(FluentCheckd24.name, FluentCheckd24);
app.component(FluentUnCheckd24.name, FluentUnCheckd24);
app.component(FluentQiTaChuRuKu24.name, FluentQiTaChuRuKu24);
app.component(FluentXiuGaiJiBenKuCun24.name, FluentXiuGaiJiBenKuCun24);
app.component(FluentCancel24.name, FluentCancel24);
app.component(FluentAdd24.name, FluentAdd24);
app.component(FluentEdit24.name, FluentEdit24);
app.component(FluentDelete28.name, FluentDelete28);
app.component(FluentComplete24.name, FluentComplete24);
app.component(FluentLock24.name, FluentLock24);

app.use(store).use(router).use(ElementPlus).use(Antd).use(components).use(Directive).mount('#app');
registerStore();

(window as any).$api = api;
let registerRouteFresh = true; // 定义标识，记录路由是否添加
router.beforeEach(async (to, from, next) => {
  //useStore.setting.$reset();
  let token = useStore.setting.token;
  if (to.path == '/login') {
    if (token) {
      next({
        path: '/',
      });
    } else {
      next();
    }
  } else {
    if (!token) {
      next({
        path: '/login',
      });
    } else {
      if (!useStore.setting.userInfo.hasOwnProperty()) {
        await window.$api.system.init().then(res => {
          const userSetting = res.data;
          useStore.setting.setUserInfo(userSetting.userInfo);
          useStore.setting.setApps(userSetting.apps);
          useStore.setting.setLicenses(userSetting.licenses);
          useStore.setting.setShortcuts(userSetting.shortcuts);
        });
        await window.$api.dictionary.getComboBoxs().then(res => {
          const comboBoxs = res.data;
          useStore.dictionary.setComboBoxs(comboBoxs);
        });
      }
      // if (!useStore.chat.signalR) {
      //   const signalR = new SignalR();
      //   await signalR
      //     .doOpen()
      //     .then(() => {
      //       console.log('连接');
      //     })
      //     .catch(e => {
      //       console.error(e);
      //     });
      //   signalR.signalr.on(MessageTypeEnum[MessageTypeEnum.NewMessage], message => {
      //     ElNotification.success({
      //       title: '您有一条新消息通知',
      //       message: '来自用户' + message.senderName,
      //     });
      //   });
      //   useStore.chat.setSignalR(signalR);
      // }
      // if (registerRouteFresh && useStore.setting.getAuthModules) {
      //   // var route = {
      //   //   path: '/',
      //   //   name: 'module',
      //   //   components: {},
      //   //   meta: {
      //   //     keepAlive: true,
      //   //   },
      //   // };
      //   // addRoute允许带children添加，所以循环第一层即可
      //   // routerPackag(route, useStore.setting.authModules);
      //   // router.options.routes[0].children.push(route);
      //   // router.addRoute('layout', route);
      //   // next({ ...to, replace: true });
      //   // registerRouteFresh = false;
      // } else {

      // }
      next();
      // if (Object.keys(store.state.user.current) <= 0) {
      //   api.user.getUserInfo().then(res => {
      //     store.commit('user/user_set', res.Data);
      //   });
      // }
      // //动态生成路由
      // if (store.state.privilege.modules.length <= 0) {
      //   api.privilege.getAuthorizeModuleOperates().then(res => {
      //     store.commit('privilege/modules_set', res.Data);
      //     setRouter(store.state.privilege.modules);
      //   });
      // } else {
      //   let flag = false; //判断是否页面中是否已经存在该路由下的tab页
      //   //options记录当前页面中已存在的tab页
      //   for (let tab of store.state.tab.tabs) {
      //     //用名称匹配，如果存在即将对应的tab页设置为active显示桌面前端
      //     if (tab.name === to.name) {
      //       flag = true;
      //       store.commit('tab/tab_active', to.path);
      //       break;
      //     }
      //   }
      //   //如果不存在，则新增tab页，再将新增的tab页设置为active显示在桌面前端
      //   if (!flag) {
      //     store.commit('tab/tab_add', {
      //       route: to.path,
      //       name: to.name,
      //     });
      //     store.commit('tab/tab_active', to.path);
      //   }
      //   store.commit('privilege/module_select', to.meta);
      // }
    }
  }
});
// const routerPackag = (route, routers) => {
//   routers.filter(item => {
//     if (item.Path && (item.Controller == 'Module' || item.Controller == 'Employee')) {
//       route.components[item.Controller] = () => import(/* @vite-ignore */ '/src/views' + item.Path + '.vue');
//     }
//     // 是否存在子集
//     if (item.Children && item.Children.length) {
//       routerPackag(route, item.Children);
//     }
//     //return true;
//   });
// };
